package com.accgm.api.filter;

import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Form;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.accgm.api.oauth.BaseFilter;
import com.accgm.api.oauth.SignatureParams;
import com.accgm.api.oauth.TokenParams;

public class SignatureValidFilter extends BaseFilter {
    public static final String CLIENT_ID = "client_id";
    public static final String RESPONSE_TYPE = "response_type";
    public static final String REDIRECT_URI = "redirect_uri";
    public static final String AUTHORIZATION_CODE = "code";
    public static final String CLIENT_SECRET = "client_secret";
    public static final String ACCESS_TOKEN = "access_token";
    public static final String GRANT_TYPE = "grant_type";
    protected SignatureParams signatureParams = new SignatureParams();
    protected TokenParams tokenParams = new TokenParams();;
   // protected UserAppGrantDao userAppGrantDao = UserAppGrantDao.getInstance();
    //protected AppDao appDao = AppDao.getInstance();
    public static int TOKEN_AVAILABLE_TIME = 60 * 60 * 24 * 30;// access
    private static Logger logger = LoggerFactory
	    .getLogger(SignatureValidFilter.class);

    // token有效时间:30天
    // public static int TOKEN_AVAILABLE_TIME = 60;// access

    protected int beforeHandle(Request request, Response response) {
	String access_token = null;
	// 从header中获取签名参数
	Form headers = (Form) request.getAttributes().get(
		"org.restlet.http.headers");
	signatureParams.setOauthConsumerKey(headers
		.getFirstValue("oauth_consumer_key"));
	signatureParams.setOauthNonce(headers.getFirstValue("oauth_nonce"));
	signatureParams.setOauthSignature(headers
		.getFirstValue("oauth_signature"));
	signatureParams.setOauthSignatureMethod(headers
		.getFirstValue("oauth_signature_method"));
	signatureParams.setOauthTimestamp(headers
		.getFirstValue("oauth_timestamp"));
	access_token = headers.getFirstValue("access_token");
	// 验证签名
	StringBuilder sb = new StringBuilder();
	sb.append(signatureParams.getOauthConsumerKey() + "&");
	sb.append(signatureParams.getOauthTimestamp() + "&");
	logger.debug("oauth_consumer_key="
		+ signatureParams.getOauthConsumerKey());
	
	/*App app = appDao.getByAppKey(signatureParams.getOauthConsumerKey());
	if (app == null) {
	    response.setEntity(unknownApp(mediaType));
	    return STOP;
	}
	sb.append(app.getAppSecret());
	String md5 = MD5.encode(sb.toString());
	Signature signature = new DefaultSignatureImpl();
	if (signature.compare(md5, signatureParams.getOauthSignature()) == false) {
	    // 验证签名失败
	    response.setEntity(invalidSignature(mediaType));
	    return STOP;
	}
	if (access_token == null || access_token.equals("")) {
	    // access token验证失败
	    response.setEntity(invalidAccessToken(mediaType));
	    return STOP;
	} else {
	    UserAppGrant uag = userAppGrantDao.getByToken(access_token);
	    if (uag == null) {
		// access token已经过期
		response.setEntity(invalidAccessToken(mediaType));
		return STOP;
	    }
	}*/
	return CONTINUE;
    }

    /*private boolean isOverdue(String timestamp) {
		long c = Long.valueOf(timestamp).longValue();
		long now = System.currentTimeMillis();
		if ((now - c) / 1000 > TOKEN_AVAILABLE_TIME) {
		    // 已经过期
		    return true;
		}
		return false;
    }*/
}
